/**
 * @ Author: luoqi
 * @ Create Time: 2024-07-23 22:25
 * @ Modified by: luoqi
 * @ Modified time: 2024-07-23 23:37
 * @ Description:
 */

#include <stdint.h>
#include "bsp.h"
#include "timer.h"

inline int timer_pwm_start(TimerDev timer, TimerChannel channel)
{
    uint32_t ch;
    switch(channel) {
    case TIMER_CHANNEL_CH1:
        ch = TIM_CHANNEL_1;
        break;
    case TIMER_CHANNEL_CH2:
        ch = TIM_CHANNEL_2;
        break;
    case TIMER_CHANNEL_CH3:
        ch = TIM_CHANNEL_3;
        break;
    default:
        return -1;
    }
    switch(timer) {
    case TIMER_DEV_TIM1:
        HAL_TIM_PWM_Start(&htim1, ch);
        HAL_TIMEx_PWMN_Start(&htim1, ch);
        break;
    default:
        return -1;
    }
    return 0;
}

inline int timer_pwm_stop(TimerDev timer, TimerChannel channel)
{
    uint32_t ch;
    switch(channel) {
    case TIMER_CHANNEL_CH1:
        ch = TIM_CHANNEL_1;
        break;
    case TIMER_CHANNEL_CH2:
        ch = TIM_CHANNEL_2;
        break;
    case TIMER_CHANNEL_CH3:
        ch = TIM_CHANNEL_3;
        break;
    default:
        return -1;
    }
    
    switch(timer) {
    case TIMER_DEV_TIM1:
        HAL_TIM_PWM_Stop(&htim1, ch);
        HAL_TIMEx_PWMN_Stop(&htim1, ch);
        break;
    default:
        return -1;
    }
    return 0;
}

inline int timer_pwm_set(TimerDev timer, TimerChannel channel, int duty)
{
    uint32_t ch;
    duty = duty > 1000 ? 1000 : duty;
    switch(channel) {
    case TIMER_CHANNEL_CH1:
        ch = TIM_CHANNEL_1;
        break;
    case TIMER_CHANNEL_CH2:
        ch = TIM_CHANNEL_2;
        break;
    case TIMER_CHANNEL_CH3:
        ch = TIM_CHANNEL_3;
        break;
    default:
        return -1;
    }
    switch(timer) {
    case TIMER_DEV_TIM1:
        __HAL_TIM_SET_COMPARE(&htim1, ch, duty);
        break;
    default:
        return -1;
    }
    return 0;
}
